package com.xsmart.goods.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.goods.dao.AttributeDao;
import com.xsmart.goods.dao.GoodsTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AttributeService {
    @Autowired
    AttributeDao attributeDao;

    /**
     * 查询商品属性
     * @param requestBean
     * @return
     */
    public ResponseBean queryAttr(RequestBean requestBean) {
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        Integer cat_id = requestBean.getRequestParamInteger("cat_id");
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        List<Map<String,Object>> attrList = attributeDao.queryTypeAttr(page,cat_id);
        for (int i = 0; i<attrList.size(); i++){
            StringBuffer attrValue = new StringBuffer();
            StringBuffer attrValueId = new StringBuffer();
            List<Map<String,Object>> attrValList = attributeDao.queryAttrVal((Long) attrList.get(i).get("attr_id"));
            for (int j = 0; j < attrValList.size(); j++){
                attrValue.append(attrValList.get(j).get("attr_value"));
                attrValueId.append(attrValList.get(j).get("attr_value_id"));
                if (j != attrValList.size()-1){
                    attrValue.append(",");
                    attrValueId.append(",");
                }
            }
            attrList.get(i).put("attr_value",attrValue);
            attrList.get(i).put("attr_value_id",attrValueId);
        }
        page.setRecords(attrList);
        responseBean.addResultPageItem("attrList", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 根据类型ID查询所有的属性值
     * @param requestBean
     * @return
     */
    public ResponseBean queryAll(RequestBean requestBean) {
        Integer cat_id = requestBean.getRequestParamInteger("cat_id");
        ResponseBean responseBean = requestBean.getResponseBean();
        List<Map<String,Object>> attrList = attributeDao.queryAll(cat_id);
        for (int i = 0; i<attrList.size(); i++){
            List<Map<String,Object>> attrVal = new ArrayList<>();
            if ("1".equals(attrList.get(i).get("attr_input_type").toString())){
                List<Map<String,Object>> attrValList = attributeDao.queryAttrVal((Long) attrList.get(i).get("attr_id"));
                for (int j = 0; j < attrValList.size(); j++){
                    Map<String,Object> map = new HashMap<>();
                    map.put("label",attrValList.get(j).get("attr_value"));
                    map.put("value",attrValList.get(j).get("attr_value_id"));
                    attrVal.add(map);
                }
            }
            attrList.get(i).put("attrVal",attrVal);
        }
        responseBean.addResultItem("attrList",attrList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 新增商品属性
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean addAttr(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Map<String, Object> attrMap = getParam(requestBean);
        List attrList = (List)requestBean.getRequestParam("attr");
        Integer attr_id = attributeDao.queryMaxId();
        attr_id = attr_id +1;
        attrMap.put("attr_id",attr_id);
        try {
            attributeDao.addAttr(attrMap);
            if(attrList.size()>0){
                attributeDao.addAttrVal(attrList,attr_id);
            }
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }

        return responseBean;
    }

    /**
     * 修改商品属性
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean modAttr(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Map<String, Object> attrMap = getParam(requestBean);
        Integer attr_id = (Integer)attrMap.get("attr_id");
        List attrList = (List)requestBean.getRequestParam("attr");
        String attr_value_id = requestBean.getRequestParamString("attr_value_id");
        String[] attrVal = attr_value_id.split(",");
        try {
            attributeDao.modAttr(attrMap);
            attributeDao.delAttrVal(attr_id);
            attributeDao.addAttrVal(attrList,attr_id);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 删除商品属性
     * @param requestBean
     * @return
     */
    @Transactional
    public ResponseBean delAttr(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer attr_id = requestBean.getRequestParamInteger("attr_id");
        try {
            attributeDao.delAttr(attr_id);
            attributeDao.delAttrVal(attr_id);
            responseBean.setResult(ResultConstant.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

    /**
     * 获取参数
     * @param requestBean
     * @return
     */
    private Map<String, Object> getParam(RequestBean requestBean){
        String[][] params = new String[][]{{"attr_id", ""},
                {"cat_id", ""},
                {"attr_name", ""},
                {"attr_input_type", "0"},
                {"attr_type", ""},
                {"attr_index", "0"},
                {"sort_order", "100"},
                {"is_linked", "0"}};
        Map<String, Object> brandMap = new HashMap<>();
        for (String[] param : params) {
            Object value = requestBean.getRequestParam(param[0]);
            if (value == null) {
                brandMap.put(param[0], param[1]);
            } else {
                brandMap.put(param[0], requestBean.getRequestParam(param[0]));
            }
        }
        return brandMap;
    }

    /**
     * 查询商品类型的id和名称
     * @param requestBean
     * @return
     */
    public ResponseBean queryGoodsTypeName(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        List<Map<String,Object>>GoodsTypeNameList=attributeDao.queryGoodsTypeName();
        responseBean.addResultItem("queryGoodsTypeName",GoodsTypeNameList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
